package com.lin.controller;

import com.lin.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

@Controller
@RequestMapping("/return")
public class returnController {
    @RequestMapping("/testString")
    public String testString(){
        System.out.println("testString执行了...");
        return "success";
    }

    @RequestMapping("/testVoid")
    public void testVoid(HttpServletRequest request, HttpServletResponse response) throws Exception{
        System.out.println("testVoid...");
        request.getRequestDispatcher("/anno.jsp").forward(request,response);
//        response.sendRedirect(request.getContextPath()+"/anno.jsp");
        return;
    }

    @RequestMapping("/testModelAndView")
    public ModelAndView testModelAndView() throws Exception{
        System.out.println("ModelAndView...");
        ModelAndView md = new ModelAndView();
        User user = new User();
        user.setUsername("张三");
        user.setPassword("123456");
        user.setBirthday(new Date());
        md.addObject("user",user);
        //跳转到那个页面,用的视图解析器
        md.setViewName("success");
        return md;
    }

    /**
     * forward和Redirect关键字
     * @return
     * @throws Exception
     */
    @RequestMapping("/testForwardAndRedirect")
    public String testForwardAndRedirect(HttpServletRequest request) throws Exception{
        System.out.println("testForwardAndRedirect...");

//        return "forward:/WEB-INF/pages/success.jsp";
        String path = request.getContextPath();
        return "redirect:/index.jsp";
    }



}
